#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <math.h>

#define INT32 int
#define DHCP_LEASE_FILE "./dhcp.leases"

#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif

int host_num = 0;
int old_host_num = 0;

typedef struct _host{
	char ip[64];
	char mac[64];
	char host[64];
	char remain[64];
}HOST_T;

typedef struct _hosts{
	int num;
	int old_num;
	HOST_T *host;
}HOSTS_T;

static HOSTS_T hosts={0, 0, NULL};

INT32 hosts_get_host_number(char *file)
{
	FILE *fp = NULL;
	char buff[512] = {0};
	char remain[64];
	char mac[64];
	char ip[64];
	char host[64];
	int read_num = 0;
	int host_num = 0;

	char MacAddress[18] = { 0 };
	char IPv4Address[16];
	char IPv6Address[32];
	char Type[16];
	char DeviceName[64];
	char NickName[64];
	char ReserveIP[16];
	char Band[16];
	char SignalLevel[16];
	char DeviceOS[16];
	char DeviceType[32];
	char DeviceFamily[32];

	fp = fopen(file, "r");
	if(fp == NULL)
		return 0;

	while(fgets(buff, sizeof(buff), fp)){
		//read_num = sscanf(buff, "%s %s %s %s *", remain, mac, ip, host);
		read_num = sscanf(buff, "%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s", 
			MacAddress, IPv4Address,IPv6Address, Type, DeviceName, NickName,
			ReserveIP, Band, SignalLevel, DeviceOS,DeviceType,DeviceFamily);
		//printf("read_num = %d\n", read_num);
		if( read_num >= 4)
			host_num++;
	}
	fclose(fp);
	
	return host_num;
}

INT32 hosts_get_host_info(char *file, int index, char *key, char *value)
{
	FILE *fp = NULL;
	char buff[512] = {0};
	char remain[64] = {0};
	char mac[64] = {0};
	char ip[64] = {0};
	char host[64] = {0};
	int read_num = 0;
	int host_idx = 0;

	char MacAddress[18] = { 0 };
	char IPv4Address[16];
	char IPv6Address[32];
	char Type[16];
	char DeviceName[64];
	char NickName[64];
	char ReserveIP[16];
	char Band[16];
	char SignalLevel[16];
	char DeviceOS[16];
	char DeviceType[32];
	char DeviceFamily[32];

	fp = fopen(file, "r");
	if(fp == NULL)
		return 0;

	while(fgets(buff, sizeof(buff), fp)){
		//read_num = sscanf(buff, "%s %s %s %s *", remain, mac, ip, host);
		read_num = sscanf(buff, "%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s", 
			MacAddress, IPv4Address,IPv6Address, Type, DeviceName, NickName,
			ReserveIP, Band, SignalLevel, DeviceOS,DeviceType,DeviceFamily);
		//printf("read_num = %d\n", read_num);
		if( read_num >= 4){
			host_idx++;
			if(host_idx == index){
				//printf("dbg---> remain=[%s], mac=[%s], ip=[%s], host=[%s]\n", remain, mac, ip, host);
				if(strstr(key, "LeaseTimeRemaining"))
					strcpy(value, remain);
				else if(strstr(key, "MACAddress"))
					strcpy(value, mac);
				else if(strstr(key, "IPAddress"))
					strcpy(value, ip);
				else if(strstr(key, "HostName"))
					strcpy(value, host);
			}
		}
	}
	fclose(fp);
	
	return 0;
}

void hosts_print()
{
	int i = 1;
	char value[64] = {0};

	for(i = 1; i <= hosts.num; i++){
		hosts_get_host_info(DHCP_LEASE_FILE, i, "LeaseTimeRemaining", value);
		printf("i = [%d], remain=[%s]\n", i, value);
		hosts_get_host_info(DHCP_LEASE_FILE, i, "MACAddress", value);
		printf("i = [%d], mac=[%s]\n", i, value);
		hosts_get_host_info(DHCP_LEASE_FILE, i, "IPAddress", value);
		printf("i = [%d], ip=[%s]\n", i, value);
		hosts_get_host_info(DHCP_LEASE_FILE, i, "HostName", value);
		printf("i = [%d], host=[%s]\n", i, value);
	}
}

INT32 hosts_update_data_model(int old_num, int num)
{
#if 0
	if(old_num > num)
		tr69c_delObjInstance("InternetGatewayDevice.LANDevice.1.Hosts.Host.", num + 1, old_num);

	if(old_num < num)
		tr69c_addObjInstance("InternetGatewayDevice.LANDevice.1.Hosts.Host.", num - old_num);
#endif
}

INT32 hosts_update_host_info()
{
	hosts.old_num = hosts.num;
	hosts.num = hosts_get_host_number(DHCP_LEASE_FILE);
	if(hosts.num != hosts.old_num)
		hosts_update_data_model(hosts.old_num, hosts.num);
}

int main(int argc, char *argv[])
{
	int index;

	hosts_update_host_info();
	//hosts_load_result(DHCP_LEASE_FILE);
	hosts_print();

	return 0;
}
